package com.example.mymatch.service.Imp;

import com.example.mymatch.dao.IUser;
import com.example.mymatch.model.User;
import com.example.mymatch.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 对服务接口规范的方法实现
 * 编写者：吴茂景
 */

@Service
public class IUserServiceImpl implements IUserService {

    @Autowired
    private IUser userMapper;   //对后端数据操作某张表的定义规范 编写者：吴茂景

    @Override//添加方法
    public int insertUser(User user){
        int userValue=0;
        //查看当前用户名称不能重复
        User user1=new User();
        user1.setId(user.getId());
        List<User> getC=userMapper.getAllUser(user1);
        if (getC.size()>=1){
            //不可以添加用户名称
            userValue=-1;
        }else {
            userValue=userMapper.insertUser(user);
        }
        return userValue;
    }

    @Override
    public List<User> getAllUser(User user){
        return userMapper.getAllUser(user);
    }

    @Override
    public int updateUser(User user){
        int userValue=0;
        //判断主键不能为0
        if (user.getId()!=0){
            //主键信息必修存在

            //名称不能重复

            userValue=userMapper.updateUser(user);
        }
        return userValue;
    }

    @Override
    public  int deleteUser(User user){
        int userValue=0;
        //判断主键不能为0
        if (user.getUserName()!=null){
            //主键是否有其他表有关联，如果关联，则先删除其他表的信息，或者给出此条信息不允许被删除


            userValue=userMapper.deleteUser(user);
        }
        return userValue;
    }

    //单条查询
    @Override
    public User getUser(User user){
       User user1=new User();
       if (user.getUserName()!=null){
           user1=userMapper.getUser(user);
       }
       return user1;
    }
}
